<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/13
 * Time: 15:29
 */

namespace app\common\model;


use think\Model;

class Bets extends Model
{
    protected $name = 'bets';

    protected $createTime = 'createtime';

    protected $append = [
        'status_name',
        'game_name',
    ];

    public function user()
    {
        return $this->belongsTo('User','user_id','id')->field('id,username');
    }
    public function lotterylol()
    {
        return $this->hasOne('LotteryLol','index','sequencenum');
    }

    public function lotterypvp()
    {
        return $this->hasOne('LotteryPvp','index','sequencenum');
    }
    public function lotterydota2()
    {
        return $this->hasOne('LotteryDota2','index','sequencenum');
    }

    public function getStatusNameAttr($value,$data)
    {
        if( $data['status'] == null && strlen($data['status']) == 0 ){
            return '待开奖';
        }
        if($data['status'] == 1){
            return '已中奖';
        }
        if($data['status'] == 0){
            return '未中奖';
        }
        if($data['status'] == 3){
            return '已撤单';
        }

    }

    public function getGameNameAttr($value,$data)
    {
        $gameConfig = Config::where('group','publiconfig')->where('name','game')->find();
        $gameNames = json_decode($gameConfig['value'],true);
        return $gameNames[$data['game']];
    }

    public static function getOrderSn($game)
    {
        $orderSn = strtoupper($game).date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        if( self::where('order_sn',$orderSn)->count() > 0){
            self::getOrderSn();
        }else{
            return $orderSn;
        }
    }
}